刷 1000 題 Coderwars LV8 或 LV7
不代表有本事刷 LV1 一題
前面練習是為了更熟悉 Ruby 語法及如何使用
重點在於如何思考邏輯、解題
接下來會漸漸把強度加強
這次以LV6、LV7各一題
Task
Given a list of digits, return the smallest number that could be formed from these digits, using the digits only once (ignore duplicates).
Notes:
Only positive integers will be passed to the function (> 0 ), no negatives or zeros.
def min_value(digits)
# your code here
end
Test.assert_equals(min_value([1, 3, 1]), 13)
Test.assert_equals(min_value([4, 7, 5, 7]), 457)
Test.assert_equals(min_value([4, 8, 1, 4]), 148)
Complete the solution so that the function will break up camel casing, using a space between words.
Example
solution('camelCasing') # => should return 'camel Casing'
def solution(string)
# complete the function
end
Test.assert_equals(solution('camelCasing'), 'camel Casing')
Test.assert_equals(solution('camelCasingTest'), 'camel Casing Test')
答案:
# Form The Minimum
def min_value(digits)
digits.uniq.sort.join.to_i
end
# Break camelCase
def solution(string)
string.gsub(/([A-Z])/, ' \1')
end
本文同步發布於 小菜的 Blog https://riverye.com/